SELECT CASE
SELECT CASE statements select zero or more of many alternatives. These multi-line
block structures have sufficient flexibility and options that they are appropriate for all
but the simplest decisions.
syntax
SELECT CASE [ALL] <test-expression>
CASE <case-expressions> ' any number of these
... zero or more statements
CASE <case-expressions> ' another one
... zero or more statements
CASE ELSE ' only one CASE ELSE
... zero or more statements
END SELECT
test expression
In the SELECT CASE statement, the test-expression can be TRUE, FALSE, a numeric
expression, or a string expression. As they are encountered, the values in the CASE
statements are compared with this test expression. For example, in a block that
begins with SELECT CASE x, the values of the subsequent CASE expressions are compared with
the value x had when SELECT CASE x was executed. In this example, x is the
test-expression.
SELECT CASE - zero or one of many
When a match between the test-expression and a case-expression is found, the code
following the CASE statement is executed, until a CASE, EXIT SELECT, CASE ELSE, or END
SELECT is executed. This is a one of many test, because at most one CASE block is
executed.
If the test expression doesn't equal any of the CASE expressions and there is no CASE ELSE, none are executed (none of many).
The last CASE statement can be CASE ELSE. If the CASE ELSE is reached, the code following it will be executed. This catch-all statement can be used to eliminate the none of many possibility, and is useful for catching unexpected conditions.
SELECT CASE ALL - n of many
The ALL option can appear in a SELECT CASE statement to create an n of many test.
The code following all CASE statements having a case-expression matching the
test-expression will be executed. CASE ELSE is not compatible with the ALL option,
as no way exists to know whether a match was found.
A CASE ALL can be used in place of CASE ELSE however, and as the name implies, the code following CASE ALL is executed if the CASE ALL is reached.